package com.dragon.www.haier.net;

import java.io.IOException;
import java.net.DatagramPacket;

/**
 * Created by Administrator on 2015/11/17 22:12.
 * Author: songxy
 * Email: thinkdata@163.com
 */
public class AppDatagramPacket implements ASocketPacket {
	/**
	 * 组网内部报文
	 */
	private DatagramPacket packet;
	/**
	 * 头部
	 */
	protected byte[] packetHeader;
	/**
	 * 类型
	 */
	protected byte[] packetType;
	/**
	 * 数据域长度
	 */
	protected byte[] packetLength;
	/**
	 * 数据域
	 */
	protected byte[] packetData;
	/**
	 * 校验值
	 */
	protected byte[] packetCS;
	/**
	 * 尾部
	 */
	protected byte[] packetEnd;

	public AppDatagramPacket() {
		this(null);
	}
	public AppDatagramPacket(DatagramPacket packet) {
		this.packetHeader = new byte[] { (byte) 0xEE, (byte) 0xFF};
		this.packetType = new byte[] { (byte) 0x03 };
		this.packetLength = new byte[4];
		this.packetEnd = new byte[] { (byte) 0xFF, (byte) 0xFF };
		this.packet = packet;
	}

	public void checksum() {
		this.packetCS = new byte[] {(byte) 0xBB};
	}

	@Override
	public void writeExternal(DatagramPacket output) throws IOException {

	}

	@Override
	public void readExternal(DatagramPacket input) throws IOException, ClassNotFoundException {

	}

	public byte[] getStart() {
		return this.packetHeader;
	}

	public byte[] getAction() {
		return packetType;
	}

	public void setAction(byte[] action) {
		this.packetType = action;
	}

	public byte[] getPacketLength() {
		return packetLength;
	}

	public void setPacketLength(byte[] data1) {
		this.packetLength = data1;
	}

	public byte[] getPacketData() {
		return packetData;
	}

	public void setPacketData(byte[] data2) {
		this.packetData = data2;
	}

	public byte[] getEnd() {
		return this.packetEnd;
	}

	public byte[] getCS() {
		return packetCS;
	}

	private int totalLength() {
		return this.packetHeader.length+this.packetType.length
				+this.packetLength.length+this.packetCS.length
				+this.packetEnd.length+this.packetData.length;
	}

	@Override
	public String toString() {
		return "Header(2)=[ " + Utils.byte2HexString(packetHeader)
				+ " ] Type(1)=[ " + Utils.byte2HexString(packetType)
				+ " ] Data Length(4)=[ " + Utils.byte2HexString(packetHeader)
				+ " ] CS=[ " + Utils.byte2HexString(packetCS)
				+ " ] End=[ " + Utils.byte2HexString(packetEnd) + "] .";
	}
}
